package com.saclub.app.fragment;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.PhoneUtils;
import com.saclub.app.R;
import com.saclub.app.activity.GuideActivity;
import com.saclub.app.activity.HomeActivity;
import com.saclub.app.activity.MyToolBarActivity;
import com.saclub.app.common.Constants.SliderTypeEnum;
import com.saclub.app.common.MyApp;
import com.saclub.app.common.PerferencesHelper;
import com.saclub.app.http.ApiRequest;

public class WelcomeFragment extends BaseProgressFragment<MyToolBarActivity> implements AnimationListener {

	@Bind(R.id.id_welcome)
	protected ViewGroup welcome;

	@Bind(R.id.id_welcome_ad_img)
	protected ImageView adImageView;

	@Bind(R.id.id_welcome_ad_img_layout)
	protected View adImageViewLayout;

	@Override
	protected int obtainContentRes() {
		return R.layout.act_welcome;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

		Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.splash_alpha_in);
		anim.setAnimationListener(this);

		welcome.startAnimation(anim);

	}

	@Override
	protected void initData() {

		RequestParams params = new RequestParams();
		params.put("type_id", SliderTypeEnum.startad.getType());
		ApiRequest.welcome_ad.request(this, params);

	}

	@Override
	public void onBackPressed() {

	}

	@OnClick(R.id.id_welcome_btn_next)
	public void clickNext() {
		goTo();
	}

	@Override
	public void onAnimationEnd(Animation anim) {

		goTo();

	}

	@Override
	public void onAnimationRepeat(Animation anim) {

	}

	@Override
	public void onAnimationStart(Animation anim) {

	}

	public ImageView getAdImageView() {
		return adImageView;
	}

	public View getAdImageViewLayout() {
		return adImageViewLayout;
	}

	private void goTo() {
		int guideVCode = PerferencesHelper.getInstance().readInt(GuideActivity.GUIDE_V_CODE_KEY);

		if (PhoneUtils.getAppVersionCode(MyApp.getInstance()) > guideVCode) {//去引导页

			Intent intent = new Intent(caller, GuideActivity.class);
			startActivity(intent);

		} else {//去首页

			HomeActivity.go(getActivity());

		}
	}

}
